import axios from 'axios';

//get请求
let myget = para => {
  axios
    .get(para.url, {
      // headers: {
      //   Authorization: localStorage.getItem('token'),
      // },
      params: para.data,
    })
    .then(function(response) {
      para.sucFun(response);
    })
    .catch(function(error) {
      para.errFun(error);
    });
};

//delete请求
let mydelete = para => {
  axios
    .delete(para.url, {
      headers: {
        Authorization: localStorage.getItem('token'),
      },
    })
    .then(function(response) {
      para.sucFun(response);
    })
    .catch(function(error) {
      para.errFun(error);
    });
};

let mypost = para => {
  axios
    .post(para.url, para.data, {
      headers: {
        Authorization: localStorage.getItem('token'),
      },
    })
    .then(function(response) {
      para.sucFun(response);
    })
    .catch(function(error) {
      para.errFun(error);
    });
};

let myput = para => {
  axios
    .put(para.url, para.data, {
      headers: {
        Authorization: localStorage.getItem('token'),
      },
    })
    .then(function(response) {
      para.sucFun(response);
    })
    .catch(function(error) {
      para.errFun(error);
    });
};

//增加key字段（antd列表用）
let addKey = list => {
  let count = 1;
  for (var i in list) {
    list[i]['key'] = count;
    count++;
  }
  return list;
};
export default {
  myget: myget,
  addKey: addKey,
  mypost: mypost,
  mydelete: mydelete,
  myput: myput,
};
